package com.study.leetcode;

public class Solution_155 {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(1);
        minStack.push(2);
        System.out.println("minStack.getMin() = " + minStack.getMin());
        minStack.push(-1);
        minStack.push(-2);
        System.out.println("minStack.getMin() = " + minStack.getMin());
        minStack.pop();
        System.out.println("minStack.getMin() = " + minStack.getMin());
        System.out.println("minStack.top() = " + minStack.top());
    }
}

class MinStack {

    ListNode listNode;

    class ListNode {
        int val = Integer.MAX_VALUE;
        ListNode next;
        int min;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
            this.min = listNode != null ? Math.min(listNode.min, val) : val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.min = listNode != null ? Math.min(listNode.min, val) : val;
            this.next = next;
        }
    }

    public MinStack() {

    }

    public void push(int val) {
        if (listNode == null) {
            listNode = new ListNode(val);
        }
        listNode = new ListNode(val, listNode);
    }

    public void pop() {
        if (listNode != null) {
            listNode = listNode.next;
        }
    }

    public int top() {
        return listNode.val;
    }

    public int getMin() {
        if (listNode != null) {
            return listNode.min;
        }
        return 0;
    }
}